package com.jc.auth.application.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jc.auth.application.convert.AuthUserDTOConverter;
import com.jc.auth.application.dto.AuthUserDTO;
import com.jc.auth.common.entity.Result;
import com.jc.auth.domain.entity.AuthUserBO;
import com.jc.auth.domain.service.AuthUserDomainService;
import com.jc.auth.infra.basic.entity.AuthUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.connection.ReactiveSubscription;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    private AuthUserDomainService authUserDomainService;

    @RequestMapping("/doLogin")
    public Result<SaResult> doLogin(@RequestParam("validCode") String validCode) {
        try {
            Preconditions.checkArgument(!StringUtils.isBlank(validCode),"验证码不能为空！");
            SaTokenInfo saTokenInfo = authUserDomainService.doLogin(validCode);
            return Result.success(saTokenInfo);
        } catch (Exception e) {
            log.error("userController.doLogin.validCode:{}", e.getMessage(),e);
            return Result.fail("用户登录失败");
        }

    }

    // 查询登录状态，浏览器访问： http://localhost:3001/user/isLogin
    @RequestMapping("/isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }

    /**
     * 用户注册
     * @param authUserDTO
     * @return
     */
    @RequestMapping("/register")
    public Result<Boolean> register(@RequestBody AuthUserDTO authUserDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("userController.register.dto:{}", JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getUserName()),"用户名不能为空");
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertUserDTOToBO(authUserDTO);
            Boolean result = authUserDomainService.register(authUserBO);
            return Result.success(result);
        } catch (Exception e) {
            log.error("userController.register.dto:{}", e.getMessage(),e);
            return Result.fail("注册用户失败");
        }

    }
    /**
     * 修改用户信息
     * @param authUserDTO
     * @return
     */
    @RequestMapping("/update")
    public Result<Boolean> update(@RequestBody AuthUserDTO authUserDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("userController.update.dto:{}", JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getUserName()),"用户名不能为空");
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertUserDTOToBO(authUserDTO);
            Boolean result = authUserDomainService.update(authUserBO);
            return Result.success(result);
        } catch (Exception e) {
            log.error("userController.update.dto:{}", e.getMessage(),e);
            return Result.fail("修改用户信息失败");
        }

    }

    /**
     * 删除用户
     * @param authUserDTO
     * @return
     */
    @RequestMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthUserDTO authUserDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("userController.delete.dto:{}", JSON.toJSONString(authUserDTO));
            }
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertUserDTOToBO(authUserDTO);
            Boolean result = authUserDomainService.delete(authUserBO);
            return Result.success(result);
        } catch (Exception e) {
            log.error("userController.update.dto:{}", e.getMessage(),e);
            return Result.fail("删除失败");
        }

    }
    /**
     * 修改用户状态
     * @param authUserDTO
     * @return
     */
    @RequestMapping("/changeStatus")
    public Result<Boolean> changeStatus(@RequestBody AuthUserDTO authUserDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("userController.changeStatus.dto:{}", JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkNotNull(authUserDTO.getStatus(),"用户状态不能为空");
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertUserDTOToBO(authUserDTO);
            Boolean result = authUserDomainService.update(authUserBO);
            return Result.success(result);
        } catch (Exception e) {
            log.error("userController.changeStatus.dto:{}", e.getMessage(),e);
            return Result.fail("删除失败");
        }

    }

    /**
     * 获取用户信息
     * @param authUserDTO
     * @return
     */
    @RequestMapping("/getUserInfo")
    public Result<AuthUserDTO> getUserInfo(@RequestBody AuthUserDTO authUserDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("userController.getUserInfo.dto:{}", JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getUserName()),"用户名不能为空");
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertUserDTOToBO(authUserDTO);
            AuthUserBO userInfo = authUserDomainService.getUserInfo(authUserBO);
            AuthUserDTO result = AuthUserDTOConverter.INSTANCE.convertUserBOToDTO(userInfo);
            return Result.success(result);
        } catch (Exception e) {
            log.error("userController.getUserInfo.dto:{}", e.getMessage(),e);
            return Result.fail("获取用户信息失败");
        }

    }

    /**
     * 用户退出
     * @param userName
     * @return
     */
    @RequestMapping("/logOut")
    public Result logOut(@RequestParam String userName){
        try {
            if(log.isInfoEnabled()){
                log.info("userController.logOut.dto:{}", JSON.toJSONString(userName));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(userName),"用户名不能为空");
            StpUtil.logout(userName);
            return Result.success();
        } catch (Exception e) {
            log.error("userController.logOut.dto:{}", e.getMessage(),e);
            return Result.fail("用户退出失败");
        }

    }


}
